﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using MaterialDemo.IService.IGoods_Category;
using MaterialDemo.IService.IGoods_Consumable;
using MaterialDemo.IService.IWorkFlow;
using MaterialDemo.Model.Goods_CategoryDTO;
using MaterialDemo.Model.Goods_ConsumableDTO;
using MaterialDemo.MVC.Filters;
using MaterialDemo.Utility;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

namespace MaterialDemo.MVC.Controllers
{
    [MySession]
    public class Goods_ConsumableController : Controller
    {
        #region 构造函数注入
        private readonly IGoods_ConsumableService _goods_ConsumableService;
        private readonly IWorkFlowService _workFlowService;
        private readonly IGoods_CategoryService _goods_CategoryService;
        private ILogger<Goods_ConsumableController> _logger;
        public Goods_ConsumableController(ILogger<Goods_ConsumableController> logger, IGoods_ConsumableService goods_ConsumableService, IWorkFlowService workFlowService, IGoods_CategoryService goods_CategoryService)
        {
            _goods_ConsumableService = goods_ConsumableService;
            _logger = logger;
            _workFlowService = workFlowService;
            _goods_CategoryService = goods_CategoryService;
        }
        #endregion

        #region 页面

        /// <summary>
        /// 耗材领用页面
        /// </summary>
        /// <returns></returns>
        public IActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 耗材申请页面
        /// </summary>
        /// <returns></returns>
        public IActionResult ApplyingIndex(int Id)
        {

            //获取耗材信息
            ConsumableOutput outputEntity = _goods_ConsumableService.LoadEntities(Id).FirstOrDefault();

            ViewBag.consumable = outputEntity;

            return View();
        }


        /// <summary>
        /// 耗材审核页面
        /// </summary>
        /// <returns></returns>
        public IActionResult ReviewIndex()
        {
            return View();
        }

        /// <summary>
        /// 耗材审核提交页面
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public IActionResult ReviewingIndex(int Id)
        {
            ViewBag.Instance = _workFlowService.LoadInstanceEntitiy(Id);
            return View();
        }

        /// <summary>
        /// 耗材出库页面
        /// </summary>
        /// <returns></returns>
        public IActionResult OutputIndex()
        {
            return View();

        }

        /// <summary>
        /// 耗材入库页面
        /// </summary>
        /// <returns></returns>
        public IActionResult InputIndex()
        {
            return View();
        }

        /// <summary>
        /// 耗材更新页面
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public IActionResult UpdateIndex(int id)
        {
           
            ConsumableOutput entity = _goods_ConsumableService.LoadEntities(id).FirstOrDefault();              
            ViewBag.onsumable = entity;
            List<Goods_CategoryOutput> list = _goods_CategoryService.GetList();
            ViewBag.category = list;
            return View();
        }
        public IActionResult Detele(int id)
        {
            var ss = _goods_ConsumableService.Detele(id);
            if (ss == true)
            {
                return Content("删除成功");
            }
            else
            {
                return Content("删除失败");
            }


        }
        public IActionResult Addgoods()
        {
            List<Goods_CategoryOutput> list = _goods_CategoryService.GetList();

          
            ViewBag.categorys = list;
            return View();
        }
        #endregion

        #region 非页面

        /// <summary>
        /// 获取耗材信息
        /// </summary>
        /// <returns></returns>
        public string GetJsonList(int page, int limit, string selectInfo)
        {

            int totalCount = 0;
            List<ConsumableOutput> outlist = _goods_ConsumableService.LoadPageEntities(page, limit, out totalCount, selectInfo);
            DataResult<List<ConsumableOutput>> dataResult = new DataResult<List<ConsumableOutput>>
            {
                code = 0,
                count = totalCount,
                msg = "ok",
                data = outlist,
            };
            return JsonNetHelper.SerializetoJson(dataResult);
        }

        /// <summary>
        /// 检查库存是否够
        /// </summary>
        /// <param name="consumablesID"></param>
        /// <param name="num"></param>
        /// <returns></returns>
        public string CheckStore(string consumablesId, double num)
        {
            if (_goods_ConsumableService.CheckStore(consumablesId, num))
            {
                return "ok";
            }
            else
            {
                return "no";
            }
        }
        public string Add(ConsumableInput input)
        {
            string userId = HttpContext.Session.GetString("UserId");

            if (_goods_ConsumableService.Add(input,userId))
            {
                return "ok";
            }
            else
            {
                return "no";
            }
        }
        /// <summary>
        /// Excel导入
        /// </summary>
        /// <returns></returns>
        public IActionResult ExcelUpload(IFormFile file)
        {
            string strResult = "导入成功！";

            string userId = HttpContext.Session.GetString("UserId");
            List<ConsumableInput> list;

            try
            {
                list = NPOIHelper.InputExcel<ConsumableInput>(file);
            }
            catch (Exception)
            {
                return Json(new
                {
                    Result = "表格数据错误！请重新检查！",
                }); ;
            }

            if (list.Count > 0)
            {
                //做非空验证
                bool flag = true;
                foreach (var item in list)
                {
                    if (string.IsNullOrEmpty(item.GoodsId) || string.IsNullOrEmpty(item.Name) || string.IsNullOrEmpty(item.Unit))
                    {
                        flag = false;
                    }
                }
                if (flag)//验证成功
                {
                    if (_goods_ConsumableService.InputExcel(list, userId))
                    {

                    }
                    else
                    {
                        strResult = "导入失败";
                    }
                }
                else
                {
                    strResult = "表格数据不符合要求";
                }
            }
            else
            {
                strResult = "表格无数据";
            }
            return Json(new
            {
                Result = strResult,
            });
        }

        /// <summary>
        /// Excel导出
        /// </summary>
        /// <returns></returns>
        public IActionResult ExcelDownload()
        {
            List<ConsumableExcelOutput> list = _goods_ConsumableService.LoadEntities();
            string[] title = {  "耗材编号", "耗材类型编号", "耗材类型名称", "名称", "规格", "库存数量", "单位", "单价", "警告数量", "添加时间" };
            byte[] buffer = NPOIHelper.OutputExcel(list,title);
            return File(buffer, "application/ms-excel", "耗材信息.xlsx");
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public string Update(ConsumableInput input)
        {
            if (_goods_ConsumableService.Upadate(input))
            {
                return "ok";
            }
            else
            {
                return "no";
            }
        }
        #endregion

    }
}